package com.primb.bench.project.approval.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.primb.baselibs.util.NetworkUtils;
import com.primb.baselibs.view.AutoToolbar;
import com.primb.bench.R;
import com.primb.bench.base.SwipeBaseActivity;
import com.primb.bench.common.constant.AppConstant;
import com.primb.bench.project.approval.entity.ApprovalListEntity;
import com.primb.bench.project.search.activity.PriceConditionActivity;
import com.primb.bench.project.search.activity.SearchDetailActivity;
import com.primb.bench.project.search.contract.SearchListContract;
import com.primb.bench.project.search.entity.PriceListEntity;
import com.primb.bench.project.search.entity.RequestPriceEntity;
import com.primb.bench.project.search.presenter.SearchListPresenterImpl;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.lankton.flowlayout.FlowLayout;

/**
 * Created by Chen on 2017/5/25.
 * 功能描述：
 */

public class ApprovalActivity extends SwipeBaseActivity implements SearchListContract.View<ApprovalListEntity.DataBean> {
    @BindView(R.id.title_bar)
    TextView titleBar;
    @BindView(R.id.tool_bar)
    AutoToolbar toolBar;
    @BindView(R.id.recycler_add_info)
    RecyclerView recyclerAddInfo;
    @BindView(R.id.iv_top)
    ImageView ivTop;
    @BindView(R.id.swipe_content)
    SwipeRefreshLayout swipeContent;
    @BindView(R.id.layout_search_condition)
    FlowLayout layoutSearchCondition;
    @BindView(R.id.layout_condition_content)
    RelativeLayout layoutConditionContent;
    private ArrayList<ApprovalListEntity.DataBean> dataList;
    private SearchListPresenterImpl mPresenter;
    private BaseQuickAdapter<ApprovalListEntity.DataBean, BaseViewHolder> quickAdapter;

    @Override
    protected int onBaseLayoutId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_search_price;
    }

    @Override
    protected void onBaseBindView() {
        titleBar.setText("待办申请");
        ivTop.setVisibility(View.GONE);
        ivTop.setImageResource(R.mipmap.ic_search_white_18dp);
        toolBar.setNavigationOnClickListener(v -> ApprovalActivity.this.finish());
        swipeContent.setColorSchemeColors(ContextCompat.getColor(this, android.R.color.holo_blue_bright), ContextCompat.getColor(this, android.R.color.holo_green_light),
                ContextCompat.getColor(this, android.R.color.holo_orange_light), ContextCompat.getColor(this, android.R.color.holo_red_light));
        swipeContent.setRefreshing(true);
        configListView();
    }

    @Override
    protected void onLoadData() {
        mPresenter = new SearchListPresenterImpl(this, this, SearchListContract.KEY_APPROVAL);
        mPresenter.start();
    }

    @Override
    public void showLoading() {
        if (swipeContent != null) {
            swipeContent.setRefreshing(true);
        }
    }

    @Override
    public void hideLoading() {
        if (swipeContent != null && swipeContent.isRefreshing()) {
            swipeContent.setRefreshing(false);
        }
    }

    @Override
    public void showError(String error) {
        showToast(error);
    }

    @Override
    public void configListView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerAddInfo.setLayoutManager(layoutManager);
        recyclerAddInfo.setHasFixedSize(true);
        recyclerAddInfo.setItemAnimator(new DefaultItemAnimator());
        dataList = new ArrayList<>();

        quickAdapter = new BaseQuickAdapter<ApprovalListEntity.DataBean, BaseViewHolder>(R.layout.item_approval, dataList) {

            @Override
            protected void convert(BaseViewHolder helper, ApprovalListEntity.DataBean item) {
                helper.setText(R.id.tv_item_approval_number, item.getPriceId());
                helper.setText(R.id.tv_item_approval_part, item.getApproveStartOrg());
                helper.setText(R.id.tv_item_approval_name, item.getApproveStartUser());
                helper.setText(R.id.tv_item_approval_date, item.getApproveStartDate());
            }
        };
        quickAdapter.setOnItemClickListener((adapter, view, position) -> {
            Intent intent = new Intent(ApprovalActivity.this, ApprovalDetailActivity.class);
            intent.putExtra("priceId", dataList.get(position).getPriceId());
            intent.putExtra("priceType", "02");
            intent.putExtra("isReapply", false);
            startActivityForResult(intent, AppConstant.APPROVAL_REQUEST);
        });
        quickAdapter.setOnLoadMoreListener(() -> {
            if (!NetworkUtils.isNetworkConnected(this)) {
                showError("无网络连接，请连接网络后重试");
                quickAdapter.loadMoreFail();
                return;
            }
            mPresenter.loadMoreList();
        }, recyclerAddInfo);
        View layoutEmpty = getLayoutInflater().inflate(R.layout.empty_list_view, (ViewGroup) recyclerAddInfo.getParent(), false);
        quickAdapter.setEmptyView(layoutEmpty);
        quickAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        quickAdapter.setNotDoAnimationCount(0);
        quickAdapter.isFirstOnly(true);
        recyclerAddInfo.setAdapter(quickAdapter);
        swipeContent.setOnRefreshListener(() -> {
            if (!NetworkUtils.isNetworkConnected(this)) {
                showError("无网络连接，请连接网络后重试");
                swipeContent.setRefreshing(false);
                return;
            }
            mPresenter.start();
        });
    }

    @Override
    public void setDataToList(List<ApprovalListEntity.DataBean> entity) {
        swipeContent.setRefreshing(false);
        dataList.clear();
        dataList.addAll(entity);
        quickAdapter.notifyDataSetChanged();
    }

    @Override
    public void loadMoreList(List<ApprovalListEntity.DataBean> entity) {
        if (entity.size() > 0) {
            quickAdapter.addData(entity);
            quickAdapter.notifyDataSetChanged();
            quickAdapter.loadMoreComplete();
        } else {
            quickAdapter.loadMoreEnd();
        }
    }

    @Override
    public void loadMoreField() {
        quickAdapter.loadMoreFail();
    }

    @Override
    public void clearListData() {
        dataList.clear();
        quickAdapter.getData().clear();
        quickAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == AppConstant.APPROVAL_REQUEST && resultCode == AppConstant.APPROVAL_RESULT) {
            mPresenter.start();
        }
    }
}
